/**
 * Created by dell on 2017/11/17.
 */
$(function () {
    laydate.render({
        elem: '[name=startDate]'
    });
    laydate.render({
        elem: '[name=endDate]'
    });
});

function report(fmt) {
    location=ctx+'/provider/report?fmt='+fmt;
}

//模供应商糊查询
function search(){
    var param={};
    param.name=$("#condition input[name='name']").val();
    param.practice=$("#condition input[name='practice']").val();
    param.startDate=$("#condition input[name='startDate']").val();
    param.endDate=$("#condition input[name='endDate']").val();
    $("#providerTable").bootstrapTable("refresh",{query:param});
}

//操作
function caozuo (){
    var cao = '<button class="layui-btn layui-btn-normal layui-btn-mini updatePro">修改</button>' +
        '<button type="button"  class="layui-btn layui-btn-danger layui-btn-mini delPro">删除</button>'+
        '<button type="button"  class="layui-btn layui-btn-info layui-btn-mini display">详情</button>';
    return cao;
}

window.optEvents={
    "click .updatePro":function (e,value,row) {
        updatePro(row);
    },
    "click .delPro":function (e,value,row) {
        delPro(row);
    },
    "click .display":function (e,value,row) {
        var proId = row.proId;
        location.href = ctx + "/provider/display?proId=" + proId;
    }
}

function initAddPro(){
    $("div#addPro").html("");
    $("div#updatePro").html("");
    $("div.myForm form").clone().appendTo("#addPro");
    $("#addPro").dialog({
        title:'添加供应商信息',
        btnValue:'添加',
        addBind:function(){
            if(!submitAll()){
                return;
            }
            var param=$("#addPro").find("form").serialize();
            $.post(ctx+"/provider/addPro",param,function(msg){
                $("#addPro").dialog("close");
                toastr.warning(msg);
                $("#providerTable").bootstrapTable("refresh");
            },'text');
        }
    });
}

function initupdatePro(){
    $("div#addPro").html("");
    $("div#updatePro").html("");
    $("div.myForm form").clone().appendTo("#updatePro");
    $("#updatePro").dialog({
        title:'修改供应商信息',
        btnValue:'修改',
        addBind:function(){
            if(!submitAll()){
                return;
            }
            var param=$("#updatePro").find("form").serialize();
            $.post(ctx+"/provider/updatePro",param,function(msg){
                $("#updatePro").dialog("close");
                toastr.warning(msg);
                $("#providerTable").bootstrapTable("refresh");
            },'text');
        }
    });
}

//添加供应商信息
function addPro() {
    $(".myForm span").html(" ");
    initAddPro();
    laydate.render({
        elem: '[name=proValidity]'
    });
    $("#addPro").dialog("open");
}

//修改供应商信息
function updatePro(row) {
    $(".myForm span").html(" ");
    initupdatePro();
    laydate.render({
        elem: '[name=proValidity]'
    });
    var proId = row.proId;
    $.post(ctx+"/provider/getPro","proId="+proId,function(pro){
        $("#updatePro").dialog("open");
        $("#updatePro form").setForm(pro);
    },'json');
}

//删除单个供应商信息
function delPro(row){
    swal({
        title:"确认提示",
        text:"确认删除选中的供应商信息?",
        type:"warning",
        showCancelButton:true,
        confirmButtonText:"删除",
        confirmButtonColor:"#dd0c12",
        cancelButtonText: "取消"
    }).then(function(){
        var proId = row.proId;
        $.post(ctx+"/provider/delPro","proId="+proId,function(msg){
            toastr.warning(msg);
            var param = {};
            param.offset=0;
            $("#providerTable").bootstrapTable("refresh",{query:param});
        },"text");
    })
}

//表单验证
function checkProNum() {

    var proNum = $("input[name=proNum]").val();
    $("span#error_proNum").html(" ").css("color","red");
    var re = /^\d{8}([A-Za-z0-9])$/;
    if(proNum==""){
        $("span#error_proNum").html('<i class="layui-icon" style="color:red;">ဇ</i>&nbsp;<span>公司编号不可以为空!</span>');
        return false;
    }
    if(!re.test(proNum)){
        $("span#error_proNum").html('<i class="layui-icon" style="color:red;">ဇ</i>&nbsp;<span>公司编号格式不正确!</span>');
        return false;
    }
    $("span#error_proNum").html('<i class="layui-icon" style="color:green;"></i>');
    return true;
}

function checkProName() {
    var proName = $("textarea[name=proName]").val();
    $("span#error_proName").html(" ").css("color","red");
    if(proName==""){
        $("span#error_proName").html('<i class="layui-icon" style="color:red;">ဇ</i>&nbsp;<span>公司名称不可以为空!</span>');
        return false;
    }
    $("span#error_proName").html('<i class="layui-icon" style="color:green;"></i>');
    return true;
}

function checkproReaddress() {
    var proReaddress=$("textarea[name=proReaddress]").val();
    $("span#error_proReaddress").html(" ").css("color","red");
    if(proReaddress==""){
        $("span#error_proReaddress").html('<i class="layui-icon" style="color:red;">ဇ</i>&nbsp;<span>公司地址不可以为空!</span>');
        return false;
    }
    $("span#error_proReaddress").html('<i class="layui-icon" style="color:green;"></i>');
    return true;
}

function checkproPerson() {
    var proPerson=$("input[name=proPerson]").val();
    $("span#error_proPerson").html(" ").css("color","red");
    if(proPerson==""){
        $("span#error_proPerson").html('<i class="layui-icon" style="color:red;">ဇ</i>&nbsp;<span>负责人不可以为空!</span>');
        return false;
    }
    $("span#error_proPerson").html('<i class="layui-icon" style="color:green;"></i>');
    return true;
}

function checkproPhone() {
    var proPhone=$("input[name=proPhone]").val();
    $("span#error_proPhone").html(" ").css("color","red");
    var phone = /^((0\d{2,3})-)(\d{7,8})(-(\d{3,}))?$/;
    var phones= /^1[358]\d{9}$/;
    if(proPhone==""){
        $("span#error_proPhone").html('<i class="layui-icon" style="color:red;">ဇ</i>&nbsp;<span>联系方式不可以为空!</span>');
        return false;
    }
    if(!phone.test(proPhone)&&!phones.test(proPhone)){
        $("span#error_proPhone").html('<i class="layui-icon" style="color:red;">ဇ</i>&nbsp;<span>格式不正确!</span>');
        return false;
    }
    $("span#error_proPhone").html('<i class="layui-icon" style="color:green;"></i>');
    return true;
}

function checkproValidity() {
    var proValidity=$("input[name=proValidity]").val();
    $("span#error_proValidity").html(" ").css("color","red");
    var dats = /^(\d{4})-(0\d{1}|1[0-2])-(0\d{1}|[12]\d{1}|3[01])$/;
    if(proValidity==""){
        $("span#error_proValidity").html('<i class="layui-icon" style="color:red;">ဇ</i>&nbsp;<span>合作日期不可以为空!</span>');
        return false;
    }
    if(!dats.test(proValidity)){
        $("span#error_proValidity").html('<i class="layui-icon" style="color:red;">ဇ</i>&nbsp;<span>日期(yyyy-MM-dd)</span>');
        return false;
    }
    $("span#error_proValidity").html('<i class="layui-icon" style="color:green;"></i>');
    return true;
}

function checkproPractice() {
    var proPractice=$("textarea[name=proPractice]").val();
    $("span#error_proPractice").html(" ").css("color","red");
    if(proPractice==""){
        $("span#error_proPractice").html('<i class="layui-icon" style="color:red;">ဇ</i>&nbsp;<span>主营业务不可以为空!</span>');
        return false;
    }
    $("span#error_proPractice").html('<i class="layui-icon" style="color:green;"></i>');
    return true;
}

function submitAll() {
    var ProNum=checkProNum();
    var ProName=checkProName();
    var proReaddress=checkproReaddress();
    var proPerson=checkproPerson();
    var proPhone=checkproPhone();
    var proValidity=checkproValidity();
    var proPractice=checkproPractice();
    if(ProNum&&ProName&&proReaddress&&proPerson&&proPhone&&proValidity&&proPractice){
        return true;
    }
    return false;
}